#include <soar.h>

void do_txn(unsigned char *msg, int *msg_size, int count)
{/*{{{*/
#ifdef _MY_MULTI
    if(count % 10000 == 0)
        printf("%d\n", count);
#else
    printf("msg_in(%d): %s\n", *msg_size, msg);
#endif

    return;
}/*}}}*/

static int loop()
{/*{{{*/
    MSGQ_SYN msgq;
    unsigned char *msg;
    int msg_size;
    int res;
    int count;

    ALLOC_MULTI_RETERR(msg, unsigned char, MSGQ_MSGSIZE_MAX, -1);

    res=msgq_syn_attach("msgq_syn_test_server", &msgq);
    if(res)
    {
        fprintf(stderr, "%s: msgq_syn_attach fail, res:%d\n",
                __func__, res);
        return(-1);
    }

    count=0;
    while(1)
    {
        res=msgq_syn_fetch(msgq, msg, &msg_size, 30*1000);
        if(res)
        {
            if(res == RET_ERR_TIMEOUT)
            {
                fprintf(stderr, "%s: msgq_syn_fetch timeout, continue\n",
                        __func__);
                continue;
            }
            fprintf(stderr, "%s: msgq_syn_fetch fail, res:%d\n",
                    __func__, res);
            goto GO_OUT;
        }
        msg[msg_size]=ZERO;

        count++;
        do_txn(msg, &msg_size, count);

        res=msgq_syn_reply(msgq, msg, msg_size, 10*1000);
        if(res)
        {
            fprintf(stderr, "%s: msgq_syn_reply fail, res:%d\n",
                    __func__, res);
            goto GO_OUT;
        }
    }

GO_OUT:
    msgq_syn_detach(msgq);
    return(0);
}/*}}}*/

int main(int argc, char *argv[])
{/*{{{*/
    loop();

    return(0);
}/*}}}*/
